from businessrules import AlbumBiz, SongBiz, ArtistBiz
from common import AlbumEntity, SongEntity, SearchFilter,ArtistEntity
from dataaccess import ArtistData
from modules import staff

def new_album():
    """/radio/staff/new_album"""
    if(request.env.request_method == "POST"):
        artist_name = request.vars.get("artist") 
        sf =  SearchFilter.SimpleFilter(ArtistEntity.ArtistEntity.ColumnNames.name,SearchFilter.ConditionType.Equal,artist_name)
        artist = ArtistData.ArtistData().SelectArtistByFilter(sf)
        artist_id = None
        if len(artist) > 0:
            artist_id = artist[0].ArtistID
        else:
            artistEntity = ArtistEntity.ArtistEntity(artistID = -1, name=artist_name)
            artist_id = ArtistBiz.ArtistBiz().InsertArtist(artistEntity)
        album = AlbumEntity.AlbumEntity(albumID=-1, artistID=artist_id, 
                albumName = request.vars.get("album"),
                promoter = request.vars.get("promoter", "Unknown"), 
                genre = request.vars.get("genre"))  
        album_id = AlbumBiz.AlbumBiz().InsertAlbum(album)

        songs = request.vars.get("songs", "")
        songs = songs.split("\n")
        for song_name in songs:
            song = SongEntity.SongEntity(songID=-1,name = song_name, visibility=0,
                    albumID = album_id)
            song_id = SongBiz.SongBiz().InsertSong(song)
        if(request.vars.get("review_now")):
            redirect(URL('reviews','submit', args=(album_id)))

    return staff.new_album()
